from django.shortcuts import render

# Create your views here.


from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from rest_framework.filters import SearchFilter, OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend

from . import models
from .serializer import CourseCategorySerializer, CourseSerializer, CourseChapterSerializer
from . import pagination


class CategoryView(GenericViewSet, ListModelMixin):
    queryset = models.CourseCategory.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class = CourseCategorySerializer


class CourseView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class = CourseSerializer

    filter_backends = [SearchFilter, OrderingFilter, DjangoFilterBackend]

    # 参与过滤的字段
    filter_fields = ['course_category']

    # 允许排序的字段
    ordering_fields = ['id', 'price', 'students']

    # 参与搜索的字段
    search_fields = ['name', 'id', 'brief']
    # 分页组件
    pagination_class = pagination.BasicPagination


class CourseChapterView(GenericViewSet, ListModelMixin):
    queryset = models.CourseChapter.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseChapterSerializer

    # filter_backends = [DjangoFilterBackend, ]
    # filter_fields = ['course']

    from .SearchBackend import CourseFilterSet
    filter_backends = [DjangoFilterBackend, ]
    filter_class = CourseFilterSet


class SearchCourseView(GenericViewSet, ListModelMixin):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseSerializer

    filter_backends = [SearchFilter, ]
    # 参与搜索的字段
    search_fields = ['name', 'brief']

    # 分页组件
    pagination_class = pagination.BasicPagination
